<?php

namespace App\Http\Controllers\Manage;

use App\Helpers\ResponseData;
use App\Helpers\ServiceTrait;
use App\Http\Controllers\Controller;
use App\Models\Announcement;
use App\Models\CustomerManager;
use App\Models\Department;
use App\Models\ShopDetail;
use App\Models\SubAccount\SubAccount;
use App\Models\SubAccount\SubAccountLoginLogoutRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

//工作台
class DashboardController extends Controller
{
    use ResponseData,ServiceTrait;
    public function list(Announcement $model,CustomerManager $customerManager,ShopDetail $detail,SubAccount $account,Request $request){
        $user = $request->user();
        $size = $request->input('size',3);
        $announce = Cache::remember('users_announcement', 60, function ()use($model,$user,$size) {
            $announce = $model->orderBy('id','desc')->paginate($size);
            foreach ($announce as $k=>$v){
                $announce[$k]['is_unread'] = $this->userIsReadAnnouncement($user->id,$v->id);
            }
        });
        if($user->data_permission){
            $customerManager = $this->model($customerManager,$user);
        }
        $customer = $customerManager->where('cooperation_status',1)->count();
        if($user->data_permission){
            $detail = $this->model($detail,$user);
        }
        $shop = $detail->where('cooperation_status',1)->count();
        if($user->data_permission){
            $shopArrModel = $this->model(new ShopDetail(),$user);
            $shop_arr = $shopArrModel->where('cooperation_status',1)->pluck('id');
        }else{
            $shop_arr = ShopDetail::query()->where('cooperation_status',1)->pluck('id');
        }

        $sub_count = 0;
        if(count($shop_arr)>0){
            $sub_count = SubAccount::query()->whereIn('shop_id',$shop_arr)->count();
        }
        return $this->success(compact('announce','customer', 'shop','sub_count'));
    }

    //今日待上号
    public function todayInfo(Request $request){
        $user = $request->user();
        $size = $request->input('size',20);
        $model = DB::table('sub_account_login_logout_records');
        $model = $model
            ->select('id','shop_name','sub_account','shift_name','department1','department2','department3','staff_name')
            ->where('log_in_out_date',date('Y-m-d',time()));
        //今日待上号
        if($user->data_permission){
            $department_arr = explode(',',$user->data_permission);
            if(count($department_arr)>0){
                $waitUpAccount = $model
                    ->whereIn('department_id',$department_arr)
                    ->paginate($size);
                $waitUpAccountDay = $model
                    ->whereNull('login_time')
                    ->whereIn('department_id',$department_arr)
                    ->where('shift_name','早班')
                    ->count();
                $waitUpAccountNight = $model
                    ->whereNull('login_time')
                    ->whereIn('department_id',$department_arr)
                    ->where('shift_name','晚班')
                    ->count();
            }

        }else{
            $waitUpAccount = SubAccountLoginLogoutRecord::query()
                ->with('shop')
                ->whereNull('login_time')
                ->where('log_in_out_date',date('Y-m-d',time()))->paginate($size);
            $waitUpAccountDay = $model
                ->whereNull('login_time')
                ->where('shift_name','早班')
                ->count();
            //        晚班剩余
            $waitUpAccountNight = $model
                ->whereNull('login_time')
                ->where('shift_name','晚班')
                ->count();
        }
        return $this->success(compact('waitUpAccount','waitUpAccountNight','waitUpAccountDay'));
    }

    //    昨日漏上号
    public function yesterdayInfo(Request $request){
        $user = $request->user();
        $size = $request->input('size',10);
        //昨日漏上号
        list($start,$end) = $this->yesterday();
        $model = DB::table('sub_account_login_logout_records');
        $model = $model
            ->select('id','shop_name','sub_account','shift_name','department1','department2','department3','staff_name')
            ->whereBetween('log_in_out_date',[$start,$end])
            ->where('is_forget_login',1);
        if($user->data_permission){
            $department_arr = explode(',',$user->data_permission);
            if(count($department_arr)>0){
                $yesterdayAccount = $model
                    ->whereIn('department_id',$department_arr)
                    ->paginate($size);
                $yesterdayAccountDay = $model
                    ->whereIn('department_id',$department_arr)
                    ->where('shift_name','早班')
                    ->count();
                $yesterdayAccountNight = $model
                    ->whereIn('department_id',$department_arr)
                    ->where('shift_name','晚班')
                    ->count();
            }
        }else{
            $yesterdayAccount = $model->paginate($size);
            $yesterdayAccountDay = $model
                ->where('shift_name','早班')
                ->count();
            $yesterdayAccountNight = $model
                ->where('shift_name','晚班')
                ->count();
        }
        return $this->success(compact('yesterdayAccount','yesterdayAccountDay','yesterdayAccountNight'));
    }
    //两天漏登记
    public function towdaysInfo(Request $request){
        $user = $request->user();
        $size = $request->input('size',20);
        //2天漏登记
        list($twostart,$twoend) = $this->yesterday();
        $model = DB::table('sub_account_login_logout_records');
        $model = $model
            ->select('id','shop_name','sub_account','shift_name','department1','department2','department3','staff_name','is_data_recorded','log_in_out_date')
            ->whereBetween('log_in_out_date',[$twostart,$twoend])
            ->where('is_forget_record',1);
        if($user->data_permission){
            $department_arr = explode(',',$user->data_permission);
            if(count($department_arr)>0){
                $twoDayAccount = $model
                    ->whereIn('department_id',$department_arr)
                    ->paginate($size);
                $twoDayAccountDay = $model
                    ->where('shift_name','早班')
                    ->whereIn('department_id',$department_arr)
                    ->count();
                $twoDayAccountNight = $model
                    ->where('shift_name','晚班')
                    ->whereIn('department_id',$department_arr)
                    ->count();
            }

        }else{
            $twoDayAccount = $model->paginate($size);
            $twoDayAccountDay = $model
                ->where('shift_name','早班')
                ->count();
            $twoDayAccountNight = $model
                ->where('shift_name','晚班')
                ->count();
        }
        return $this->success(compact('twoDayAccount','twoDayAccountDay','twoDayAccountNight'));
    }
    //七日内待续费子账号
    public function senvenInfo(Request $request){
        $user = $request->user();
        $size = $request->input('size',20);
        //七天
        $beginSevenDays = $this->sevenDays();
        $model = DB::table('sub_accounts');
        $model = $model
            ->join('shop_details','sub_accounts.shop_id','=','shop_details.id')
            ->join('customer_managers','shop_details.customer_id','=','customer_managers.id')
            ->select('sub_accounts.id',
                'shop_details.shop_name',
                'sub_accounts.sub_account',
                'customer_managers.customer_login_name as customer_name',
                'sub_accounts.service_time_start',
            'sub_accounts.service_time_end')
            ->whereBetween('service_time_end',[$beginSevenDays,now()]);
        if($user->data_permission){
            $department_arr = explode(',',$user->data_permission);
            if(count($department_arr)>0){
                $sevenWaitAccount = $model
                    ->whereIn('sub_accounts.department_id',$department_arr)
                    ->paginate($size);
            }

        }else{
            $sevenWaitAccount = $model->paginate($size);
        }
//        if($user->data_permission){
//            $sevenWaitAccountCountModel = $this->model(new SubAccount(),$user);
//            $sevenWaitAccountCount = $sevenWaitAccountCountModel->whereBetween('service_time_end',[$beginSevenDays,now()])->count();
//        }else{
//            $sevenWaitAccountCount = SubAccount::query()->whereBetween('service_time_end',[$beginSevenDays,now()])->count();
//        }
        return $this->success($sevenWaitAccount);
    }
    //店铺未更新状态
    public function unreadUpdate(Request $request){
        $user = $request->user();
        $size = $request->input('size',10);
        $department = Department::query()->pluck('name','id');
        if($user->department_id==1){//超级管理员
            $model = DB::table('shop_update_histories');
            $model = $model
                ->join('shop_details','shop_update_histories.shop_manage_id','=','shop_details.id')
                ->select('shop_update_histories.id','shop_update_histories.department_one_id',
                'shop_update_histories.department_two_id','shop_update_histories.department_three_id',
                    'shop_update_histories.shop_manage_id','shop_update_histories.operator_time','shop_details.shop_name');
            $res = $model
                ->paginate($size);
            $shopUnread = $this->filterArray($res,$department);
        }elseif($user->data_permission){
            $department_arr = explode(',',$user->data_permission);
            if(count($department_arr)>0){
                $model = DB::table('shop_records');
                $model = $model
                    ->join('shop_details','shop_records.shop_manage_id','=','shop_details.id')
                    ->select('shop_records.id','shop_records.department_one_id',
                        'shop_records.department_two_id','shop_records.department_three_id',
                        'shop_records.shop_manage_id','shop_records.operator_time','shop_details.shop_name');
                $res = $model
                    ->whereIn('shop_records.department_id',$department_arr)
                    ->where('shop_records.is_read',1)
                    ->groupBy('shop_records.operator_time')
                    ->paginate($size);
                $shopUnread = $this->filterArray($res,$department);
            }
        }else{
            //店铺更新未看情况
            $model = DB::table('shop_records');
            $model = $model
                ->join('shop_details','shop_records.shop_manage_id','=','shop_details.id')
                ->select('shop_records.id','shop_records.department_one_id',
                    'shop_records.department_two_id','shop_records.department_three_id',
                    'shop_records.shop_manage_id','shop_records.operator_time','shop_details.shop_name');
            $res = $model
                ->where('shop_records.is_read',1)
                ->groupBy('shop_records.operator_time')
                ->paginate($size);
            $shopUnread = $this->filterArray($res,$department);
        }
        return $this->success($shopUnread);
    }
}
